//
//  TimetableViewController.m
//  IhuayuanClient
//
//  Created by 黄思清 on 2017/6/26.
//  Copyright © 2017年 com.ihuayuan. All rights reserved.
//

#import "TimetableViewController.h"
#import "CalendarView.h"
#import "DatePickerView.h"
#import "NSDate+Extension.h"
#import "PopWindowView.h"
#import "NSObject+Tips.h"


@interface TimetableViewController ()
@property (nonatomic , strong) CalendarView  * CalendarView;
@property (nonatomic , strong) UILabel *dateLabel;
@property (nonatomic , strong) NSMutableArray *monthArr;
@property (nonatomic , strong) NSString *year;
@property (nonatomic , strong) NSString *month;
@property(nonatomic,strong)UILabel *bottomL;

@end

@implementation TimetableViewController

-(UILabel *)bottomL
{
    if (!_bottomL) {
        _bottomL = [[UILabel alloc]init];
        _bottomL.text = @"*请假说明：如需请假，请点击对应日期";
        _bottomL.textColor = UIColors(0xFF0000);
        _bottomL.font = [UIFont systemFontOfSize:14];
    }
    return _bottomL;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:nil];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgation];
    [self.view addSubview:self.bottomL];
    [self.bottomL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-5);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

//    _cid = @"";
    _year = @"";
    _month = @"";
    
    _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, MAIN_SCREEN_WIDTH, 80)];
    
    NSDate *date =[NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth = [[formatter stringFromDate:date]integerValue];
    NSString *currentMonths = (currentMonth < 10)?([NSString stringWithFormat:@"0%ld",currentMonth]):[NSString stringWithFormat:@"%ld",currentMonth];
    [_dateLabel setText:[NSString stringWithFormat:@"%ld年%@月",currentYear,currentMonths]];
    [_dateLabel setTextColor:[UIColor blackColor]];
    [_dateLabel setTextAlignment:1];
    [_dateLabel setFont:[UIFont systemFontOfSize:20]];
    [self.view addSubview:_dateLabel];


    self.CalendarView = [[CalendarView alloc] initWithFrame:CGRectMake(0, 144, MAIN_SCREEN_WIDTH, 500) colorArr:@[[UIColor whiteColor],[UIColor cyanColor],[UIColor greenColor],[UIColor redColor]] textOrBackGround:0 clickDateBlock:^(NSString *Date, NSIndexPath *indexpath)
    {
        for (NSDictionary * time in self.monthArr)
        {

            NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat setDateFormat:@"yyyy-M-d"];//设定时间格式,这里可以设置成自己需要的格式
            NSDate *Mydate =[dateFormat dateFromString:Date];

            NSDateFormatter* dateFormat1 = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象

            [dateFormat1 setDateFormat:@"yyyyMd"];//设定时间格式,这里可以设置成自己需要的格式
            NSString *currentDateStr = [dateFormat1 stringFromDate:Mydate];

            NSString *currentDateStr1 = [currentDateStr stringByReplacingOccurrencesOfString:@"-" withString:@""];

            if ([currentDateStr1 isEqualToString:time[@"ymd"]]){
                if([time[@"isleave"] isEqualToString:@"1"]){
                    [self.navigationController showMessageTips:@"该日已经请过假了！"];
                }else{
                    PopWindowView *poppicker = [[PopWindowView alloc]initWithCompleteBlock:^(NSString *conText)
                    {
                        [self askForleaveRequest:conText aid:time[@"aid"]];
                    }];
                    poppicker.themeColor = [UIColor blueColor];
                    poppicker.title        = [NSString stringWithFormat:@"%@%@",Date,@"课程请假"];
                    poppicker.placehoder   = @"请输入请假理由";
                    poppicker.themeColor   =  UIColors(MainColor);
                    [poppicker show];
                }
                return ;
            }
        }
    }];
    _monthArr = [[NSMutableArray alloc]init];
    [self initTimetableHttp];
    [self.view addSubview:self.CalendarView];
}

// 请假的网络请求
-(void)askForleaveRequest:(NSString *)text aid:(NSString *)aid
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCoursetable&a=askForleave" parmDic:@{@"sid":self.sid,@"aid":aid,@"remark":text} requestType:RequestPOST Success:^(id data)
    {
        [HUD showText:@"请假成功" AfterTime:1];
    } error:^(NSError *error) {

    }];
}


//设置导航
-(void)setNavgation{
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"nav_bg"]];
    UIView *navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 64)];
    [navigationBar setBackgroundColor:bgColor];
    //左侧按钮
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 44, 44)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftOnClick) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:leftBtn];
    
    //中间信息
    UILabel *titLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, MAIN_SCREEN_WIDTH, 44)];
    [titLabel setTextColor:[UIColor whiteColor]];
    [titLabel setTextAlignment:1];
    [titLabel setText:@"我的课表"];
    [navigationBar addSubview:titLabel];
    // 右侧按钮
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(MAIN_SCREEN_WIDTH-50, 20, 44, 44)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"datetime"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightOnClick) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:rightBtn];
    
    [self.view addSubview:navigationBar];
}

-(void)initTimetableHttp{

    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCoursetable&a=index" parmDic:@{@"cid":self.cid,@"year":self.year,@"month":self.month} requestType:RequestGET Success:^(id data) {
        self.monthArr = data;
        
        [self.CalendarView reloadCalendarViewWithArr:self.monthArr color:[UIColor cyanColor]];
    } error:^(NSError *error) {

    }];
}

-(void)leftOnClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)rightOnClick{


    DatePickerView *datepicker = [[DatePickerView alloc]initWithCompleteBlock:^(NSDate *selectDate) {
        [self.CalendarView reloadCalendarViewWithDate:[selectDate stringWithFormat:@"yyyy-MM-dd"]];
        
        [_dateLabel setText:[selectDate stringWithFormat:@"yyyy年MM月"]];
        
        self.year = [selectDate stringWithFormat:@"yyyy"];
        
        self.month = [selectDate stringWithFormat:@"MM"];
        
        [self initTimetableHttp];
        
    }];
    datepicker.themeColor = [UIColor blueColor];
    datepicker.datePickerStyle = DateStyleShowYearMonth;
    datepicker.dateType     = DateTypeStartDate;
    datepicker.title        = @"选择年月";
    datepicker.themeColor   = UIColors(MainColor);
    datepicker.minLimitDate = [NSDate date:@"2017-01-01" WithFormat:@"yyyy-MM-dd"];
    datepicker.maxLimitDate = [NSDate date:@"2025-01-01" WithFormat:@"yyyy-MM-dd"];
    [datepicker show];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
